package com.example.sudokugame.models

import com.google.gson.annotations.SerializedName

// 数独题目响应
data class SudokuPuzzleResponse(
    val success: Boolean,
    val data: SudokuPuzzleData
)

data class SudokuPuzzleData(
    val puzzle: Array<IntArray>,
    val solution: Array<IntArray>,
    val difficulty: String,
    val id: String
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as SudokuPuzzleData

        if (!puzzle.contentDeepEquals(other.puzzle)) return false
        if (!solution.contentDeepEquals(other.solution)) return false
        if (difficulty != other.difficulty) return false
        if (id != other.id) return false

        return true
    }

    override fun hashCode(): Int {
        var result = puzzle.contentDeepHashCode()
        result = 31 * result + solution.contentDeepHashCode()
        result = 31 * result + difficulty.hashCode()
        result = 31 * result + id.hashCode()
        return result
    }
}

// 游戏提交请求
data class GameSubmitRequest(
    val puzzle: Array<IntArray>,
    val solution: Array<IntArray>?,
    val difficulty: String,
    val score: Int,
    val time: Long,
    val completed: Boolean
)

// 游戏提交响应
data class GameSubmitResponse(
    val success: Boolean,
    val data: GameSubmitData
)

data class GameSubmitData(
    @SerializedName("gameId")
    val gameId: Int,
    val message: String,
    val completed: Boolean? = null,
    val score: Int? = null,
    val time: Long? = null,
    val difficulty: String? = null,
    val rank: RankInfo? = null
)

// 排名信息
data class RankInfo(
    val rank: Int,
    val total: Int,
    val percentage: Int
)

// 排行榜数据
data class LeaderboardResponse(
    val success: Boolean,
    val data: LeaderboardData
)

data class LeaderboardData(
    val leaderboard: List<LeaderboardEntry>,
    val type: String,
    val pagination: Pagination?
)

data class LeaderboardEntry(
    val id: Int,
    @SerializedName("player_name")
    val playerName: String?,
    val score: Int,
    @SerializedName("time_spent")
    val timeSpent: Long,
    val difficulty: String,
    @SerializedName("created_at")
    val createdAt: String
)

// 游戏历史
data class GameHistoryResponse(
    val success: Boolean,
    val data: GameHistoryData
)

data class GameHistoryData(
    val games: List<GameHistoryEntry>,
    val pagination: Pagination
)

data class GameHistoryEntry(
    val id: Int,
    val difficulty: String,
    val score: Int,
    @SerializedName("time_spent")
    val timeSpent: Long,
    val completed: Boolean,
    @SerializedName("created_at")
    val createdAt: String
)

data class Pagination(
    val page: Int,
    val limit: Int,
    val total: Int,
    val pages: Int
)

// 游戏统计响应
data class GameStatsResponse(
    val success: Boolean,
    val data: GameStatsData
)

data class GameStatsData(
    val overall: OverallStats,
    val byDifficulty: List<DifficultyStats>
)

data class OverallStats(
    @SerializedName("total_games")
    val totalGames: Int,
    @SerializedName("completed_games")
    val completedGames: Int,
    @SerializedName("best_score")
    val bestScore: Int,
    @SerializedName("best_time")
    val bestTime: Long
)

data class DifficultyStats(
    val difficulty: String,
    @SerializedName("total_games")
    val totalGames: Int,
    @SerializedName("completed_games")
    val completedGames: Int,
    @SerializedName("avg_score")
    val avgScore: Double,
    @SerializedName("avg_time")
    val avgTime: Double,
    @SerializedName("games_by_difficulty")
    val gamesByDifficulty: Int
)

// 验证响应
data class ValidateResponse(
    val success: Boolean,
    val data: ValidateData
)

data class ValidateData(
    @SerializedName("isValid")
    val isValid: Boolean,
    val message: String
)

// API基础响应
data class BaseResponse(
    val success: Boolean,
    val error: String? = null
)

// ========== 对战相关模型 ==========

// 创建对战请求
data class CreateBattleRequest(
    val difficulty: String = "medium"
)

// 对战响应
data class BattleResponse(
    val success: Boolean,
    val data: BattleData
)

data class BattleData(
    val battleId: Int,
    val puzzle: Array<IntArray>,
    val difficulty: String,
    val status: String,
    val message: String
)

// 对战信息响应
data class BattleInfoResponse(
    val success: Boolean,
    val data: BattleInfo
)

data class BattleInfo(
    val battleId: Int,
    val puzzle: Array<IntArray>,
    val difficulty: String,
    val status: String,
    val winner: String?,
    val createdAt: String,
    val startedAt: String?,
    val finishedAt: String?
)

// 完成对战请求
data class CompleteBattleRequest(
    val playerId: String,
    val score: Int,
    val time: Long
)

// 完成对战响应
data class BattleCompleteResponse(
    val success: Boolean,
    val data: BattleCompleteData
)

data class BattleCompleteData(
    val battleId: Int,
    val winner: String,
    val score: Int,
    val time: Long,
    val message: String
)

// 等待中的对战房间响应
data class WaitingBattlesResponse(
    val success: Boolean,
    val data: WaitingBattlesData
)

data class WaitingBattlesData(
    val battles: List<WaitingBattle>
)

data class WaitingBattle(
    val id: Int,
    val difficulty: String,
    @SerializedName("created_at")
    val createdAt: String
)

// 游戏难度枚举
enum class Difficulty(val value: String, val displayName: String) {
    EASY("easy", "简单"),
    MEDIUM("medium", "中等"),
    HARD("hard", "困难"),
    EXPERT("expert", "专家")
}

// 游戏状态
enum class GameStatus {
    NOT_STARTED,
    PLAYING,
    PAUSED,
    COMPLETED,
    FAILED
}

// 单元格状态
data class SudokuCell(
    var value: Int = 0,
    val isFixed: Boolean = false,
    var isError: Boolean = false,
    var isHinted: Boolean = false
) {
    val isEmpty: Boolean
        get() = value == 0
}

// 游戏状态数据
data class GameState(
    val puzzle: Array<Array<SudokuCell>>,
    val difficulty: Difficulty,
    val puzzleId: String,
    var status: GameStatus = GameStatus.NOT_STARTED,
    var startTime: Long = 0,
    var elapsedTime: Long = 0,
    var score: Int = 0,
    var hintsUsed: Int = 0,
    var errorsCount: Int = 0
) {
    fun isCompleted(): Boolean {
        for (row in puzzle) {
            for (cell in row) {
                if (cell.isEmpty || cell.isError) {
                    return false
                }
            }
        }
        return true
    }
    
    fun getElapsedSeconds(): Long {
        return if (status == GameStatus.PLAYING) {
            (System.currentTimeMillis() - startTime) / 1000 + elapsedTime
        } else {
            elapsedTime
        }
    }
} 